package Middle.FindLongestPalindrome;

import org.junit.Test;

public class FindLongestPalindrome1 {
    public static void main(String[] args) {
        String s = "xaabacxcabaaxcabaax";
        System.out.println(longestPalindrome(s));
    }

    public static String longestPalindrome(String str) {
        int start = 0;
        int end = 0;
        for (int i = 0; i < str.length(); i++) {
            int len1 = expandAroundCenter(str, i, i);
            int len2 = expandAroundCenter(str, i, i + 1);
            int len = Math.max(len1, len2);
            if (len > (end - start)) {
                end = i + len / 2;
                start = i - (len - 1) / 2;
            }
        }
        return str.substring(start, end + 1);
    }

    public static int expandAroundCenter(String str, int i, int j) {
        //运算的时候，一直到最后得到相等的时候，两个边界都会再运行一次，所以两个值都大了，最后结果-1
        while (i >= 0 && j < str.length() && str.charAt(i) == str.charAt(j)) {
            i--;
            j++;
        }
        return j - i - 1;
    }


    @Test
    public void test() {
        String s = "ccc";
        System.out.println(s.charAt(-1));
    }
}
